package com.hopu.v1.service.admin.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.exception.BusinessException;
import com.hopu.v1.pojo.classes.ClassCourseProgress;
import com.hopu.v1.pojo.system.Announcement;
import com.hopu.v1.service.admin.system.AnnouncementService;
import com.hopu.v1.mapper.admin.system.AnnouncementMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
* @author DELL
* @description 针对表【s_announcement】的数据库操作Service实现
* @createDate 2024-10-10 17:04:03
*/
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
    implements AnnouncementService{

    @Resource
    private AnnouncementMapper announcementMapper;

    /**
     * 查询所有公告
     * @param announcementTitle
     * @param announcementStatus
     * @return
     */
    @Override
    public List<Announcement> selectAnnouncement(String announcementTitle, Integer announcementStatus) {
        return announcementMapper.selectAll(announcementTitle, announcementStatus);
    }

    /**
     * 添加公告
     * @param announcement
     */
    @Override
    public void insertAnnouncement(Announcement announcement) {
        LambdaQueryWrapper<Announcement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Announcement::getAnnouncementTitle, announcement.getAnnouncementTitle());
        Announcement announcement1 = getOne(lambdaQueryWrapper);
        if(announcement1!=null){
            throw new BusinessException("公告名已存在！不能重复");
        }
        save(announcement);
    }

    /**
     * 修改公告
     * @param announcement
     */
    @Override
    public void updateAnnouncement(Announcement announcement) {
        LambdaQueryWrapper<Announcement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Announcement::getAnnouncementTitle, announcement.getAnnouncementTitle());
        Announcement announcement1 = getOne(lambdaQueryWrapper);
        announcement.setAnnouncementId(announcement.getAnnouncementId());
        if(announcement1!=null && !Objects.equals(announcement1.getAnnouncementId(), announcement.getAnnouncementId())){
            throw new BusinessException("公告名已存在！不能重复");
        }
        updateById(announcement);
    }

    /**
     * 公告标题验重
     * @param announcement
     * @return
     */
    @Override
    public boolean checkAnnouncementTitle(Announcement announcement) {
        List<Announcement> list = list(new LambdaQueryWrapper<Announcement>().eq(Announcement::getAnnouncementTitle, announcement.getAnnouncementTitle()));
        if (list == null){
            return false;
        }
        for (Announcement value : list) {
            if (Objects.equals(value.getAnnouncementId(), announcement.getAnnouncementId())) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void deleteAnnouncement(List<Integer> ids) {
        announcementMapper.deleteByIds(ids);
    }

    @Override
    public void updateStatus(Announcement announcement) {
        if (announcement.getAnnouncementId() == null) {
            throw new BusinessException("公告ID不能为空");
        }
        announcement.setAnnouncementStatus(1);
        announcement.setAnnouncementReleased(LocalDate.now());
        announcementMapper.updateById(announcement);
    }

    @Override
    public List<Announcement> select(Integer announcementStatus) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Announcement::getAnnouncementStatus, announcementStatus);

        return announcementMapper.selectList(queryWrapper);
    }
}




